package com.dapeis.web.wx.controller.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.core.utils.StringUtil;
import com.dapeis.domain.Coupons;
import com.dapeis.service.CouponsService;
import com.dapeis.service.StylistCityService;
import com.dapeis.web.wx.controller.ICouponsController;
import com.dapeis.web.wx.controller.RestController;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/wx/coupons")
public class CouponsController extends RestController implements
	ICouponsController {

    @Resource
    CouponsService couponsService;
    @Resource
    StylistCityService stylistCityService;

    @Override
    public @ResponseBody JsonElement getMyCoupons() {
	List<Coupons> coupons = couponsService
		.getMyCoupons(getThreadLocalUserId());
	JsonObject json = success();
	json.addProperty("couponsCode", StringUtil.isBlank(getThreadLocalUser()
		.getCouponsCode()) ? new String() : getThreadLocalUser()
		.getCouponsCode());
	json.addProperty("userId", getThreadLocalUserId());
	json.add("data", AGsonBuilder.create().toJsonTree(coupons));
	return json;
    }

    @Override
    public @ResponseBody JsonElement exchange(@RequestParam String couponsCode) {
	AgAssert.isTrue(
		!(StringUtil.isBlank(couponsCode) || couponsCode.length() == 0 || couponsCode
			.length() != 6), SysTipsException.class, "优惠码格式不正确!");
	Coupons coupons = couponsService.exchange(couponsCode,
		getThreadLocalUserId());
	JsonObject json = success();
	json.add("data", AGsonBuilder.create().toJsonTree(coupons));
	return json;
    }

}
